home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / insp110d.zip / DUTILSRC.ZIP / MKDESC.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  3KB  |  153 lines

  1. /*
  2.  * $Id: mkdesc.c 1.1 92/09/19 19:01:39 davidn Exp Locker: davidn $
  3.  * make descriptions file from articles in the current directory
  4.  *
  5.  * $Log:    mkdesc.c $
  6.  * Revision 1.1  92/09/19  19:01:39  davidn
  7.  * Initial revision
  8.  *
  9.  */
  10.  
  11. # include <stdio.h>
  12. # include <stddef.h>
  13. # include <stdlib.h>
  14. # include <ctype.h>
  15. # include <string.h>
  16. # include <memory.h>
  17. # include <dos.h>
  18. # include <errno.h>
  19.  
  20. # include "dosdir.h"
  21. # ifndef MAX_PATH
  22. #  define MAX_PATH 128
  23. # endif
  24.  
  25.  
  26. # define DESCFILE "files.bbs"
  27. # define ADDDOT   0
  28.  
  29. char descfile[MAX_PATH] = DESCFILE;
  30.  
  31. int line = 0;
  32. const char *hdr[] =
  33. {
  34.     "Subject:",
  35.     "Newsgroups:",
  36.     "To:",
  37.     "From:",
  38.     "Path:"
  39.     "Keywords:",
  40.     "Summary:",
  41.     NULL
  42. };
  43.  
  44. const int hln[] =
  45. {
  46.     8,
  47.     11,
  48.     3,
  49.     5,
  50.     5,
  51.     9,
  52.     8
  53. };
  54.  
  55. static char
  56. *skip_whi (char *s)
  57. {
  58.  
  59.     while (isspace (*s))
  60.         ++s;
  61.     return s;
  62. }
  63.  
  64.  
  65.  
  66. int
  67. main (int argc, char *argv[])
  68. {
  69.     int             j, carg;
  70.     FILE            *ip, *fp;
  71.     struct FFIND    ff;
  72.  
  73.     for (carg = 0; ++carg < argc; )
  74.     {
  75.         if (argv[carg][0] == '-')
  76.         {
  77.             switch (tolower (argv[carg][1]))
  78.             {
  79.                 case 's':   line = 0;   break;
  80.                 case 'n':   line = 1;   break;
  81.                 case 't':   line = 2;   break;
  82.                 case 'f':   line = 3;   break;
  83.                 case 'p':   line = 4;   break;
  84.                 case 'k':   line = 5;   break;
  85.                 case 'u':   line = 6;   break;
  86.             }
  87.         }
  88.     }
  89.  
  90.     fp = NULL;
  91.     j = FINDFIRST (ALLFILES, _A_NORMAL, &ff);
  92.     while (j == 0)
  93.     {
  94.         if (strcmp (f_name(&ff), descfile) != 0)
  95.         {
  96.  
  97. # if ADDDOT
  98.             if (strchr (f_name(&ff), '.') == NULL)
  99.                 strcat (f_name(&ff), ".");
  100. # endif
  101.             ip = fopen (f_name(&ff), "r");
  102.             if (ip != NULL)
  103.             {
  104.                 int lines, gotone;
  105.                 char buf[512];
  106.  
  107.                 for (lines = gotone = 0; !gotone && fgets (buf, 512, ip) != NULL; )
  108.                 {
  109.                     gotone = (memcmp (buf, hdr[line], hln[line]) == 0);
  110.                     if (*buf == '\n')    /* Empty line */
  111.                         break;
  112.                 }
  113.  
  114.                 if (gotone)
  115.                 {
  116.                     if (fp == NULL)
  117.                     {
  118.                         char * pp, bak[MAX_PATH];
  119.  
  120.                         strcpy (bak, descfile);
  121.                         pp = strrchr (bak, '.');
  122.                         if (pp == NULL)
  123.                             pp = bak + strlen(bak);
  124.                         strcpy (pp, ".BAK");
  125.                         remove (bak);
  126.                         rename (descfile, bak);
  127.                         fp = fopen (descfile, "w");
  128.                         if (fp == NULL)
  129.                         {
  130.                             fprintf (stderr, "mkdesc: Can't create '%s' = %s\n", descfile, strerror(errno));
  131.                             rename (bak, descfile);
  132.                             break;
  133.                         }
  134.                     }
  135.  
  136.                     fprintf (fp, "%s %s", f_name(&ff), skip_whi (buf + hln[line]));
  137.                     printf (".");
  138.                 }
  139.                 fclose (ip);
  140.             }
  141.         }
  142.         j = FINDNEXT (&ff);
  143.     }
  144.     FINDCLOSE (&ff);
  145.     if (fp != NULL)
  146.     {
  147.         fclose (fp);
  148.         printf ("\n");
  149.     }
  150.     return 0;
  151. }
  152.  
  153.